<template>
  <a-modal :title="titile" :width="900" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk"
    @cancel="handleCancel" :okButtonProps="{ props: { disabled: disabled } }" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row style="width: 100%;">
          <a-col :span="24 / 2">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title" label="消息标题">
              <a-input placeholder="请输入标题" :disabled="disabled" v-model="model.title" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24 / 2">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="selectedUser" label="通知人群">
              <a-select mode="multiple" placeholder="请选择用户" :labelInValue="true" v-model="selectedUser"
                :disabled="disabled" @dropdownVisibleChange="selectUserIds" @change="handleChange">
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row style="width: 100%;">
          <a-col :span="24">
            <a-form-model-item :labelCol="labelColX1" :wrapperCol="wrapperColX1" label="内容" class="j-field-content">
              <j-editor v-model="model.content" :disabled="disabled"></j-editor>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
    <select-user-list-modal ref="UserListModal" @choseUser="choseUser"></select-user-list-modal>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { getAction } from '@/api/manage'
import JDate from '@/components/jeecg/JDate'
import JEditor from '@/components/jeecg/JEditor'
import SelectUserListModal from './SelectUserListModal'
import moment from 'moment'

export default {
  components: { JEditor, JDate, SelectUserListModal },
  name: 'SysAnnouncementModal',
  data() {
    return {
      titile: '操作',
      visible: false,
      disableSubmit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      },
      labelColX1: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperColX1: {
        xs: { span: 24 },
        sm: { span: 21 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [{ required: true, message: '请输入标题!' }],
        selectedUser: [{ required: true, message: '请选择通知人群!' }]
      },
      url: {
        queryByIds: '/sys/user/queryByIds',
        add: '/sys/message/sysMessage/sendMsgAndPush',
        edit: '/sys/annountCement/edit'
      },
      userType: false,
      userIds: [],
      selectedUser: [],
      disabled: false,
      msgContent: '',
      userList: []
    }
  },
  created() { },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.model = {}
      this.disable = false
      this.visible = true

      this.getUser(record)
    },
    getUser(record) {
      this.model = Object.assign({}, record)
      if (record.id) {
        this.disabled = true
        this.model.title = record.esTitle
        this.model.content = record.esContent
      }
      console.log(this.model)
      this.$forceUpdate()
      // 指定用户
      if (record) {
        this.userType = true
        this.userIds = record.esReceiver
        getAction(this.url.queryByIds, { userIds: this.userIds }).then(res => {
          if (res.success) {
            //update--begin--autor:wangshuai-----date:20200601------for：系统公告选人后，不能删除------
            var userList = []
            for (var i = 0; i < res.result.length; i++) {
              var user = {}
              user.label = res.result[i].realname
              user.key = res.result[i].id
              userList.push(user)
            }
            this.selectedUser = userList
            //update--begin--autor:wangshuai-----date:20200601------for：系统公告选人后，不能删除------
            this.$refs.UserListModal.edit(res.result, this.userIds)
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.selectedUser = []
      this.visible = false
      this.$refs.form.resetFields()
    },
    handleOk() {
      const that = this
      //当设置指定用户类型，但用户为空时，后台报错
      if (this.userType && !(this.userIds != null && this.userIds.length > 0)) {
        this.$message.warning('通知人群不能为空！')
        return
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.userType) {
            this.model.toUser = this.userIds
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.resetUser()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.visible = false
      this.$emit('close')
      this.$refs.form.resetFields()
      this.resetUser()
    },
    resetUser() {
      this.userType = false
      this.userIds = []
      this.selectedUser = []
      this.disabled = false
      this.$refs.UserListModal.edit(null, null)
    },
    selectUserIds() {
      this.$refs.UserListModal.add(this.selectedUser, this.userIds)
    },

    // 子modal回调
    choseUser: function (userList) {
      this.selectedUser = []
      this.userIds = []
      for (var i = 0; i < userList.length; i++) {
        //update--begin--autor:wangshuai-----date:20200601------for：系统公告选人后，不能删除------
        var user = {}
        user.label = userList[i].realname
        user.key = userList[i].id
        this.selectedUser.push(user)
        //update--end--autor:wangshuai-----date:20200601------for：系统公告选人后，不能删除------
        this.userIds += userList[i].id + ','
      }
    },

    handleChange(userList) {
      if (userList) {
        this.userIds = []
        var users = []
        for (var i = 0; i < userList.length; i++) {
          var user = {}
          user.id = userList[i].key
          user.realname = userList[i].label
          this.userIds += userList[i].key + ','
          users.push(user)
        }
      }
      this.$refs.UserListModal.edit(users, this.userIds)
    }
  }
}
</script>

<style scoped></style>
